
<template>
  <el-container>
    <!--左侧-->
    <el-aside width="200px">
      <img src="../assets/loginBox.svg">
      <p class="imgTitle">欢迎来到动力云客系统</p>
    </el-aside>
    <!--右侧-->
    <el-main>
      <div class="loginTile">欢迎登录</div>

      <el-form ref="loginRefForm" :model="user" label-width="120px" :rules="loginRules">
        <el-form-item label="账号" prop="loginAct">
          <el-input v-model="user.loginAct" />
        </el-form-item>
        <el-form-item label="密码" prop="loginPwd">
          <el-input v-model="user.loginPwd" type="password"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登 录</el-button>
        </el-form-item>
        <el-form-item>
          <el-checkbox label="记住我" v-model="user.rememberMe" />
        </el-form-item>
      </el-form>
    </el-main>

  </el-container>
</template>

<script>
import {defineComponent} from "vue";
import {doGet, doPost} from "../http/httpRequest.js";
import {getTokenName, messageTip, removeToken} from "../util/util.js";

//<script> 标签的写法，并不是vue3写法
//js 变量方法都写在这里面
export default defineComponent({
  name: "LoginView",

  //定义变量
  data(){
    return{
      user:{},
      //定义管理规则，注意并不是定义变量，只是定义此变量名的规则
      loginRules:{
        loginAct:[
          {required:true,message:'请输入登录账号',trigger:'blur'}
        ],
        loginPwd:[
          {required:true,message:'请输入登录密码',trigger:'blur'},
          {min:6,max:16,message:'登录密码长度为6-16位',trigger:'blur'}
        ]
      }
    }
  },

  //页面渲染dom元素后会触发调用该函数（函数钩子）
  mounted() {
    this.freeLogin();
  },

  //定义方法
  methods:{
    login(){
      //验证输入框的合法性，利用Ele自带的功能
      this.$refs.loginRefForm.validate((isValid)=>{
        if(isValid){
          let formData = new FormData();
          formData.append("loginAct",this.user.loginAct);
          formData.append("loginPwd",this.user.loginPwd);
          formData.append("rememberMe",this.user.rememberMe);

          doPost("/api/login", formData).then( (resp) => {
            console.log(resp);
            if (resp.data.code === 200) {
              //登录成功，提示一下
              messageTip("登录成功", "success");

              //删除一下历史localStorage和sessionStorage中存储的token
              removeToken();

              //前端存储jwt
              //localStorage 代表整个浏览器有 信息
              //sessionStorage 只有当前标签页有 信息，从当前页面跳转也有信息
              if (this.user.rememberMe === true) {
                window.localStorage.setItem(getTokenName(), resp.data.data);
              } else {
                window.sessionStorage.setItem(getTokenName(), resp.data.data);
              }

              //跳转到系统的主页面
              window.location.href = "/dashboard";

            } else {
              //登录失败，也提示一下
              messageTip("登录失败", "error");
            }
          });
        }
      })
    },

    //用户免登录
    freeLogin(){
      let token = window.localStorage.getItem(getTokenName());
      if(token){
        doGet("/api/freeLogin",{}).then(resp => {
          if(resp.data.code === 200){
            //token 验证通过了，可以进行免登录
            window.location.href = "/dashboard";
          }
        })
      }
    }
  }
})
</script>

<style scoped>
.el-aside {
  background: #1a1a1a;
  width: 40%;
  text-align: center;
}
.el-main {
  height: calc(100vh);
}
img {
  height: 413px;
}
.imgTitle {
  color: #f9f9f9;
  font-size: 28px;
}
.el-form {
  width: 60%;
  margin: auto;
}
.loginTile {
  text-align: center;
  margin-top: 100px;
  margin-bottom: 25px;
  font-weight: bold;
}
.el-button {
  width: 100%;
}
</style>